package com.advert.provider.util;

import com.advert.model.advert.ImageBase;
import com.advert.model.advert.ImageEntity;
import com.advert.model.advert.ImageLobsEntity;
import com.advert.model.security.AccessMode;
import com.advert.provider.ProviderBase;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.core.Events;


@Name("ImageProcessingProvider")
@Scope(ScopeType.APPLICATION)
@AutoCreate
public class ImageProcessingProvider extends ProviderBase {

    public ImageLobsEntity getImageLobs(Integer imageId) {
        return entityManager.find(ImageLobsEntity.class, imageId);
    }

    public void removeImage(ImageEntity image) {
        Events.instance().raiseEvent(com.advert.util.Event.ENTITY_ACCESS, image, AccessMode.DELETE);

        entityManager.createNamedQuery("ImageEntity.removeById").
                setParameter("id", image.getId()).
                executeUpdate();
    }

    public ImageBase createImage(ImageBase imageBase) {
        Events.instance().raiseEvent(com.advert.util.Event.ENTITY_ACCESS, imageBase, AccessMode.ADD);

        entityManager.persist(imageBase);
        return imageBase;
    }
}
